<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {debug} function plugin
 *
 * Type:     function<br>
 * Name:     debug<br>
 * Date:     July 1, 2002<br>
 * Purpose:  popup debug window
 * @link http://smarty.php.net/manual/en/language.function.debug.php {debug}
 *       (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string output from {@link Smarty::_generate_debug_output()}
 */

function doScore($what,$diff){
  if($diff==0){
    $lien="<em>$what</em>";
  }else if ($diff>0){
    $lien="<strong>$what</strong>";
  }else{
    $lien = $what;
  }
  return $lien;
}

function doEquipe($id,$diff,$truncate=0){
  $lien=getEquipeLien($id,null,null,null,array('truncate'=>$truncate));
  return doScore($lien, $diff);
}

function score($params)
{
  $pars = array(
    'sepEquipe'     =>  '<span class="vs">vs</span>'
    ,'sepScore'     =>  '<span class="sep">-</span>'
    ,'truncate'     =>  0
    ,'notPlayed'    =>  null
    ,'updateScore'  =>  true
    ,'match_id'     =>  0
  );
  
  $pres[]  = '<span class="score">';
  $pres[]  =   '<span class="score_A">%s</span>';
  $pres[]  =   '%s';
  $pres[]  =   '<span class="score_B">%s</span>';
  $pres[]  = '</span>';  
  
  $p = array_merge($pars,$params);
 
  $ret  = array();
  $diff = FALSE;
  if(isset($p['scoreA']) && isset($p['scoreB'])){
    $diff = $p['scoreA'] - $p['scoreB'];
  }
  
  $hidden='';
  
  if($diff===false){
    return '';
  }
  
  if(($p['scoreA']==0 && $p['scoreB']==0) && $p['notPlayed']!==false ){
    
    if($p['notPlayed']==null){
      $p['notPlayed'] = sprintf(join($pres),0,$p['sepScore'],0);
    }
    
    $ret[]=  str_replace('"score"','"score hidden"',$p['notPlayed']);
  } else {
    if(isset($p['equipeA']) && $p['equipeA']>0){
      $ret[]=doEquipe($p['equipeA'],$diff,$p['truncate']);
    }

    if(isset($p['equipeB']) && $p['equipeB']>0){
      if(count($ret)>0){ 
        $ret[]=$p['sepEquipe'];
      }
      $ret[]=doEquipe($p['equipeB'],($diff*-1),$p['truncate']);
    }

    if(count($ret)==0){
      $out=join($pres);
      $ret[] = sprintf($out,doScore($p['scoreA'], $diff),$p['sepScore'],doScore($p['scoreB'], ($diff*-1)));
    }
  }
  
  $classes  = array("loli-score");
  $admin    = array();
  
  if($p['updateScore']==true && $p['match_id']>0){
    $classes[]="loli-update-score";
    $classes[]="match-id-".$p['match_id'];
    
    $admin[]= widget::render('form', array('type'=>'updateScore','match_id'=>$p['match_id']));
    
  } 
  
  $ret = sprintf('<div class="%s">%s %s</div>',join(' ',$classes),join('',$ret),join('',$admin));
  
  
  return $ret;

}

function smarty_function_score($params, &$smarty){
  return score($params);
}

/* vim: set expandtab: */

?>
